package com.jketing.tag;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

import com.jketing.Constants;
import com.jketing.bean.PageBean;

/**
 * PageTag 分页标签处理类
 * 
 * 使用SpringData的Page封装分页数据
 * 
 * @author  李荣华
 * @version v1.0
 * @date        2017-10-31
 * @Servlet   3.1
 * @Jsp-api   2.3.1
 * @Spring   4.3.8
 */
public class PageTag extends TagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public PageTag() {
	}

	public int doStartTag() throws JspException{
		HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
		JspWriter out = pageContext.getOut();
		if(request.getAttribute(Constants.PAGEBEAN) == null)
			return Tag.SKIP_BODY;
		try{
			StringBuffer sb = null;
			PageBean<?> p = (PageBean<?>)request.getAttribute(Constants.PAGEBEAN);
			String pageSize = (String)request.getAttribute(Constants.PAGESIZE);
			int totalPage = p.getTotalPage();
			long resultSize = p.getResultSize();
			int currentPage = p.getCurrentPage();
			sb = new StringBuffer();
			if (totalPage < 2 & resultSize >=0)
			{
				if(resultSize == 0)
					sb.append("当前的您还没有这类信息的记录。&nbsp;&nbsp;");
				else
					sb.append("当前总的有&nbsp;&nbsp;<b>"  + resultSize + "</b>&nbsp;&nbsp;条记录。&nbsp;&nbsp;");
				out.print("<td>"+sb.toString()+"</td>");
				return Tag.SKIP_BODY;
			}		
			
			sb.append("<script language=\"javascript\">");
			sb.append("var lastPageCount="+totalPage+";");
			sb.append("var upPageCount="+(currentPage-1)+";");
			sb.append("var nextPageCount="+(currentPage+1)+";");
			sb.append("function firstPage(){");
			sb.append("document.forms[\"queryForm\"].page.value='1';");
			sb.append("document.forms[\"queryForm\"].submit();}");
			sb.append("function lastPage(){");
			sb.append("document.forms[\"queryForm\"].page.value=lastPageCount;");
			sb.append("document.forms[\"queryForm\"].submit();}");
			sb.append("function upPage(){");
			sb.append("document.forms[\"queryForm\"].page.value=upPageCount;");
			sb.append("document.forms[\"queryForm\"].submit();}");
			sb.append("function nextPage(){");
			sb.append("document.forms[\"queryForm\"].page.value=nextPageCount;");
			sb.append("document.forms[\"queryForm\"].submit();}");
			sb.append("function goPage(obj){");
			sb.append("var pNo = obj.options[obj.options.selectedIndex].value;");
			sb.append("if (pNo<1 || pNo > lastPageCount){");
			sb.append("alert(\"你输入的页码必须在[1-\"+lastPageCount+\"]之间.\");return}");
			sb.append("document.forms[\"queryForm\"].page.value=pNo;");
			sb.append("document.forms[\"queryForm\"].submit();}");
			sb.append("</script>");
			
			if(currentPage > 1)
				sb.append("<td align='center'><a href='javascript:firstPage()'>&nbsp;首页&nbsp;</a></td>\n");
			else
				sb.append("<td align='center'>&nbsp;首页&nbsp;</td>\n");
			if(currentPage > 1)
				sb.append("<td align='center'><a href='javascript:upPage()'>&nbsp;上一页&nbsp;</a></td>\n");
			else
				sb.append("<td align='center'>&nbsp;上一页&nbsp;</td>\n");
			
			if(totalPage == currentPage)
				sb.append("<td align='center'>&nbsp;下一页&nbsp;</td>\n");
			else
				sb.append("<td align='center'><a href='javascript:nextPage()'>&nbsp;下一页&nbsp;</a></td>\n");
			if(totalPage == currentPage)
				sb.append("<td align='center'>&nbsp;末页&nbsp;</td>\n");
			else
				sb.append("<td align='center'><a href='javascript:lastPage()'>&nbsp;末页&nbsp;</a></td>\n");
			
			sb.append("<td align='center'>&nbsp;页码</td>\n");
			sb.append("<td align='center'>\n");
			sb.append("<select onchange='goPage(this)' id='pageSelect' name='pageSelect' class='pageSelect'>\n");
			for(int i=0;i < totalPage;i++){
				int page = i + 1;
				if(page == currentPage){
					sb.append("<option selected=true value='" + page + "'>" + page + "/" + totalPage + "</option>\n");
				}else{
					sb.append("<option value='" + page + "'>" + page + "/" + totalPage + "</option>\n");
				}
			}
			sb.append("</select>\n</td>\n");
			sb.append("<td align='center'>&nbsp;共<font color='red'><b>"+resultSize+"</b></font>条&nbsp;每页</td>\n");
			sb.append("<td align='center'>\n");
			sb.append("<select onchange='goPageSize(this)' id='pageSize' name='pageSize' class='pageSelect'>\n");
			String [] array = {"5","10","15","30","50","100"};
			for(int i=0;i < array.length;i++){
				if(array.equals(pageSize)){
					sb.append("<option selected=true value='" + array[i] + "'>" + array[i] + "</option>\n");
				}else{
					sb.append("<option value='" + array[i] + "'>" + array[i] + "</option>\n");
				}
			}
			sb.append("</select>\n</td>\n");
			out.print(sb.toString());
		}catch(Exception e)
		{
			e.printStackTrace();
			throw new JspException("Netware fail.");
		}
		return Tag.SKIP_BODY;
	}
}
